<template>
  <page-layout>
    <a-row :gutter="[10, 10]">
      <!-- 查询表单 -->
      <a-col :span="24">
        <a-card>
          <pro-query
            :searchParam="searchParam"
            @on-search ="search"
          ></pro-query>
        </a-card>
      </a-col>
      <!-- 岗位列表 -->
      <a-col :span="24">
        <a-card>
          <pro-table
            ref="tableRef"
            :fetch="fetch"
            :columns="columns"
            :toolbar="toolbar"
            :operate="operate"
            :param="state.param"
            :pagination="pagination"
            :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }"
          >
          </pro-table>
        </a-card>
      </a-col>
    </a-row>
    <save :visible="state.visibleSave" @close="closeSave"></save>
    <edit :visible="state.visibleEdit" @close="closeEdit" :record="state.recordEdit"></edit>
    <info :visible="state.visibleInfo" @close="closeInfo" :record="state.recordInfo"></info>
  </page-layout>
</template>

<script>
import save from './modal/save';
import edit from './modal/edit';
import info from './modal/info';
import { message , modal} from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import {reactive, createVNode, ref} from 'vue';
import {page, clean, remove, removeBatch, getLMModule} from "@/api/module/laboratory";

const removeKey = "remove";
const removeBatchKey = "removeBatch";

export default {
  components: {
    save,
    edit,
    info,
  },
  setup() {
    const tableRef = ref();
    /// 列配置
    const columns = [
      { dataIndex: "id", key: "id", title: "ID" },
      { dataIndex: "goodsname", key: "goodsname", title: "物品名称" },
      { dataIndex: "goodssum", key: "goodssum", title: "物品总数"},
      { dataIndex: "number", key: "number", title: "剩余可用数量"},
      { dataIndex: "type", key: "type", title: "类型" },
      { dataIndex: "unit", key: "unit", title: "单位" },
      { dataIndex: "departmentcharge", key: "departmentcharge", title: "负责角色" },
      { dataIndex: "createtime", key: "createtime", title: "创建时间" },
      { dataIndex: "updatetime", key: "updatetime", title: "修改时间" },
      { dataIndex: "updatename", key: "updatename", title: "修改人" },
    ];

    /// 数据来源
    const fetch = async (param) => {
      var response = await page(param);
      return {
        total: response.data.total,
        data: response.data.record,
      };
    };

    /// 删除库存
    const removeMethod = (record) => {
      modal.confirm({
        title: '您是否确定要删除此库存?一旦删除不可更改！！！',
        icon: createVNode(ExclamationCircleOutlined),
        okText: '确定',
        cancelText: '取消',
        onOk() {
          message.loading({ content: "提交中...", key: removeKey });
          remove({"id":record.id}).then((response) => {
            if(response.success){
              message.success({content: "删除成功", key: removeKey, duration: 1})
              tableRef.value.reload();

            }else{
              message.error({content: "删除失败", key: removeKey, duration: 1})
              tableRef.value.reload();

            }
          })
        }
      });
    }

    const removeBatchMethod = (ids) => {
      modal.confirm({
        title: '您是否确定要删除选择库存?一旦删除不可更改！！！',
        icon: createVNode(ExclamationCircleOutlined),
        okText: '确定',
        cancelText: '取消',
        onOk() {
          message.loading({ content: "提交中...", key: removeBatchKey });
          removeBatch({"ids":ids}).then((response) => {
            if(response.success){
              message.success({content: "删除成功", key: removeBatchKey, duration: 1})
              tableRef.value.reload();

            }else{
              message.error({content: "删除失败", key: removeBatchKey, duration: 1})
              tableRef.value.reload();

            }
          })
        }
      });
    }

    /// 工具栏
    const toolbar = [
      { label: "新增", event: function () { state.visibleSave = true }},
      { label: "删除", event: function () { removeBatchMethod(state.selectedRowKeys) }},
    ];

    /// 行操作
    const operate = [
      { label: "申请", event: function (record) { state.visibleInfo = true, state.recordInfo = record }},
      { label: "修改", event: function (record) { state.visibleEdit = true, state.recordEdit = record }},
      { label: "删除", event: function (record) { removeMethod(record) }},
    ];

    /// 分页参数
    const pagination = {
      pageNum: 1,
      pageSize: 10,
    }

    /// 外置参数 - 当参数改变时, 重新触发 fetch 函数
    const state = reactive({
      selectedRowKeys: [],
      param: {},
      visibleSave: false,
      visibleEdit: false,
      visibleInfo: false,
      recordEdit: {},
      recordInfo: {},
      typeList: [],
      sysDeptList: [],
    })

    const searchParam = [
      { key: "goodsName", type: "input", label: "物品名称"},
      // { key: "state", type: "select", label: "物品类型", value: "",
      //   options: [
      //     { text: "全部", value: ""},
      //     { text: "成功", value: true},
      //     { text: "失败", value: false}
      //   ]
      // },
      // { key: "state", type: "select", label: "负责角色", value: "",
      //   options: [
      //     { text: "全部", value: ""},
      //     { text: "成功", value: true},
      //     { text: "失败", value: false}
      //   ]
      // },
    ]

    const search = function(value) {
      state.param = value
    }

    const closeSave = function(){
      tableRef.value.reload();
      state.visibleSave = false
    }

    const closeEdit = function(){
      tableRef.value.reload();
      state.visibleEdit = false
    }

    const closeInfo = function(){
      tableRef.value.reload();
      state.visibleInfo = false
    }

    const onSelectChange = selectedRowKeys => {
      state.selectedRowKeys = selectedRowKeys;
    };
    // 修改查询所有资源模块
    const loadPost = () => {
      getLMModule({}).then((response)=>{
        console.log("response--",response.data)
        state.typeList = response.data.typeList;
        state.unitList = response.data.unitList;
        state.sysDeptList = response.data.sysDeptList;
      })
    }
    /// 加载 资源模块
    // loadPost();

    return {
      state, // 状态共享
      fetch, // 数据回调
      toolbar, // 工具栏
      columns, // 列配置
      operate, // 行操作
      pagination, // 分页配置
      tableRef,

      search,
      searchParam, // 查询参数

      closeSave,
      closeEdit,
      closeInfo,

      onSelectChange
    };
  },
};
</script>
